package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.constant.CommonsConstant;
import com.contest.entity.UserInfo;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import com.contest.util.UserLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.contest.entity.BettingRecord;
import com.contest.service.BettingRecordService;

import javax.servlet.http.HttpServletRequest;


/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2017-12-20 16:30:36
 */

@Controller
@RequestMapping("/bettingRecord")
public class BettingRecordController {

    @Autowired
    BettingRecordService bettingRecordService;
    @Autowired
    IdWorker idWorker;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String, Object> param) {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<BettingRecord> mPage = new Page<BettingRecord>(page, rows);


        Wrapper<BettingRecord> w = new EntityWrapper<>();
        String o = String.valueOf(param.get("name"));
        if(o != null && !o.equals("") && !o.equals("null")) {
            String name = String.valueOf(o);
            w.like("fd_user_name",name);
            //w.addFilter("fd_name={0}",name);
        }

        w.orderBy("fd_betting_time",false);
        mPage = bettingRecordService.selectPage(mPage,w);
        return new PageUtils(mPage.getRecords(), mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(BettingRecord bettingRecord) {
        bettingRecord.setFdId(idWorker.nextId() + "");
        if (bettingRecordService.insert(bettingRecord)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        BettingRecord bettingRecord = bettingRecordService.selectById(fdId);
        model.addAttribute("bettingRecord", bettingRecord);
        return "bettingRecord/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (bettingRecordService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(BettingRecord bettingRecord) {
        if (bettingRecordService.updateById(bettingRecord)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }


    //个人竞猜记录
    @RequestMapping("/client/quizRecord")
    @ResponseBody
    public R listPersonal(@RequestParam Map<String, Object> param, HttpServletRequest request) {
        UserInfo userInfo = UserLoginUtil.getCurrentUserInfo(request);
        Wrapper<BettingRecord> w = new EntityWrapper<BettingRecord>();
        w.addFilter("fd_user_id", userInfo.getFdId());
        w.orderBy("fd_betting_time", false);
        String result = (String) param.get("result");
        String gameName = (String) param.get("gameName");
        String raceName = (String) param.get("fd_race_name");
        if (result != null && result.length() > 0) {
            w.addFilter("fd_result={0}", result);
        }
        if (gameName != null && gameName.length() > 0) {
            w.addFilter("fd_game_name={0}", gameName);
        }
        if (raceName != null && raceName.length() > 0) {
            w.addFilter("fd_race_name={0}", raceName);
        }
        if(w == null){
            return R.ok(bettingRecordService.selectList(null));
        }
        return R.ok(bettingRecordService.selectList(w));
    }


    //当日个人下注排行
    @RequestMapping("/client/todayTop")
    @ResponseBody
    public R listByFee() {
        List<BettingRecord> bettingRecords = bettingRecordService.todayTop();
        return R.ok(bettingRecords);
    }


    //当日战队下注排行
    @RequestMapping("/client/todayClanTop")
    @ResponseBody
    public R todayClanTop() {
        List<BettingRecord> bettingRecords = bettingRecordService.todayClanTop();
        return R.ok(bettingRecords);
    }

    //按总场次排行
    @RequestMapping("/client/clanTotalWin")
    @ResponseBody
    public R clanTotalWin(){
        List<Map<String,Object>> res = bettingRecordService.clanTotalWin();
        return R.ok(res);
    }

    @RequestMapping("/client/indexSysInfo")
    @ResponseBody
    public R indexSysInfo(){
        EntityWrapper<BettingRecord> bettingRecordEntityWrapper = new EntityWrapper<BettingRecord>();
        bettingRecordEntityWrapper.addFilter("fd_is_settlement = {0} and fd_result={1}",1,1);
        bettingRecordEntityWrapper.orderBy("fd_betting_time",false);
        List<BettingRecord> recordList = bettingRecordService.selectList(bettingRecordEntityWrapper);
        return R.ok(recordList);
    }



}
